package com.walter.quickstart;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;

import javax.inject.Inject;
import javax.validation.constraints.NotBlank;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

@Path("/hello")
public class GreetingResource {

    private static Logger logger = Logger.getLogger(GreetingResource.class);

    @ConfigProperty(name = "greeting.message")
    String message;

    @ConfigProperty(name = "greeting.upper-case", defaultValue = "true")
    boolean upperCase;

    @Inject
    GreetingConfiguration greetingConfiguration;

    @Inject
    GreetingService service;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        //return "Hello RESTEasy!!!";
        return message + service.getGreeting();
    }

    @GET
    @Path("hello1")
    @Produces(MediaType.TEXT_PLAIN)
    public String hello1() {
        //return "Hello RESTEasy!!!";
        return "hello1";
    }

    @GET
    @Path("/greeting")
    @Produces(MediaType.TEXT_PLAIN)
    public String greeting() {
        //return "Hello RESTEasy!!!";
        return service.getGreeting();
    }

    @GET
    @Path("/optional")
    @Produces(MediaType.TEXT_PLAIN)
    public String helloOptional() {
        //return "Hello RESTEasy!!!";
        return upperCase ? message.toUpperCase() : message;
    }

    @Inject
    Config config;
    @GET
    @Path("/config")
    @Produces(MediaType.TEXT_PLAIN)
    public String hellConfig() {
        config.getPropertyNames().forEach(p -> System.out.println("=========>" + p));
        return config.getValue("greeting.message", String.class);
    }

    @ConfigProperty(name = "greeting.color")
    String color;

    @GET
    @Path("/color")
    @Produces(MediaType.TEXT_PLAIN)
    public String color() {
        return color;
    }

    @GET
    @Path("/log")
    @Produces(MediaType.TEXT_PLAIN)
    public String hellLog() {
        logger.info("=======>log");
        return "log";
    }

//    @GET
//    @Produces(MediaType.TEXT_PLAIN)
//    public String hello(@Context UriInfo uriInfo,
//                        @QueryParam("order") Order order,
//                        @NotBlank @HeaderParam("authorization") String authorization) {
//        return String.format("URI: %s - Order %s - Authorization: %s",
//        uriInfo.getAbsolutePath(), order, authorization);
//    }

    public static enum Order {
        desc, asc;
    }
}

